//
//  DGGPullDownMenuButton.m
//  demo
//
//  Created by 周家民 on 2020/8/12.
//  Copyright © 2020 mac. All rights reserved.
//

#import "DGGPullDownMenuButton.h"

@implementation DGGPullDownMenuButton

- (CGFloat)animateDuration {
    if (_animateDuration <= 0) {
        _animateDuration = 0.2;
    }
    return _animateDuration;
}

- (UIFont *)titleFont {
    if (!_titleFont) {
        _titleFont = [UIFont systemFontOfSize:14];
    }
    return _titleFont;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupUI];
}

- (void)setupUI {
    self.imageEdgeInsets = UIEdgeInsetsMake(0, 5.5, 0, 0);
    self.tintColor = [UIColor colorWithRGBA:102 g:102 b:102 a:1];
    UIImage *image = [UIImage imageNamed:@"ChooseCollctionMenuBundle.bundle/img_s_menuxiala"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [self setImage:image forState:UIControlStateNormal];
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    CGSize size = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.titleFont} context:nil].size;
    CGFloat titleX = 0.5 * (contentRect.size.width - size.width);
    return CGRectMake(titleX, 0.5 * (contentRect.size.height - size.height), size.width, size.height);
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    CGFloat imageX = self.imageEdgeInsets.left + CGRectGetMaxX([self titleRectForContentRect:contentRect]);
    return CGRectMake(imageX, 0.5 * (contentRect.size.height - self.currentImage.size.height), self.currentImage.size.width, self.currentImage.size.height);
}

- (void)setHighlighted:(BOOL)highlighted {
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    [UIView animateWithDuration:self.animateDuration
                     animations:^{
                         if (selected) {
                             self.tintColor = [UIColor colorWithRGBA:14 g:187 b:184 a:1];
                             self.imageView.transform = CGAffineTransformMakeRotation(M_PI);
                         } else {
                             self.tintColor = [UIColor colorWithRGBA:102 g:102 b:102 a:1];
                             self.imageView.transform = CGAffineTransformIdentity;
                         }
                     }];
}

@end
